package com.lanou.dllo.shise.com.lanou.dllo.fragment;


import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;

import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.dllo.shise.R;
import com.lanou.dllo.shise.com.lanou.dllo.adapter.Close_attentionListAdapter;
import com.lanou.dllo.shise.com.lanou.dllo.adapter.ViewPagerAdapter;
import com.lanou.dllo.shise.com.lanou.dllo.entity.DataBean;
import com.lanou.dllo.shise.com.lanou.dllo.entity.FoodZineBean;
import com.lanou.dllo.shise.com.lanou.dllo.entity.ResultBean;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.logging.LogRecord;

/**
 * A simple {@link Fragment} subclass.
 */
public class Close_attentionFragment extends Fragment {
    private ViewPager viewPager;//广告栏部分
    private ViewPagerAdapter viewPagerAdapter;//广告栏适配器
    private RecyclerView fragmentattentionView;//列表部分
    private RequestQueue queue;
    private Close_attentionListAdapter adapter;//定义列表内容适配器
    private Handler handler;//定义一个handler对象,实现广告轮播的过程
    private int currentItem = 0;
    private int maxItem = 0;
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            //设置当前左右滑动的下标为1,即第二张图片
            currentItem++;
            maxItem = viewPagerAdapter.getCount();
            viewPager.setCurrentItem(currentItem % maxItem);
            handler.postDelayed(runnable, 4000);

        }
    };


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_close_attention, null);
        viewPager = (ViewPager) v.findViewById(R.id.viewPager);
        fragmentattentionView = (RecyclerView) v.findViewById(R.id.fragmentattentionView);
        return v;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();

    }


    private void initView() {
        viewPagerAdapter = new ViewPagerAdapter(getFragmentManager());
        viewPager.setAdapter(viewPagerAdapter);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 1);
        fragmentattentionView.setLayoutManager(gridLayoutManager);
        adapter = new Close_attentionListAdapter(getActivity());
        fragmentattentionView.setAdapter(adapter);
        handler = new Handler();

    }

    private void initData() {
        queue = Volley.newRequestQueue(getActivity());
        String path = "http://api.meishixing.com/search/nearby/recommandplace/lng=121.53974&lat=38.88286&page=1&session_id=00001227993a5661b25da19c79c2a7c9a6affc&distance=1000&food_category_name=&price=0~200000";
        StringRequest stringRequest = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ResultBean resultBean = gson.fromJson(response, ResultBean.class);
                if (resultBean != null) {
                    List<ResultBean.ResultEntity> resultEntities = resultBean.getResult();
                    if (resultEntities != null && resultEntities.size() > 0) {
                        adapter.addResult(resultEntities);
                    }

                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }

            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }
        };
        queue.add(stringRequest);

        String url = "http://api.meishixing.com/foodzine/weekly/list/city_id=7&state=2&page=1";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                FoodZineBean foodBean = gson.fromJson(response, FoodZineBean.class);
                if (foodBean != null) {
                    List<FoodZineBean.ResultEntity> fr = foodBean.getResult();
                    if (fr != null && fr.size() > 0) {
                        viewPagerAdapter.addData(fr);
                        handler.postDelayed(runnable, 4000);//2秒之后改变viewPager的下标

                    }

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }

            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }
        };
        queue.add(request);

    }


}



